package com.sikao.app.ui;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.sikao.app.R;
import com.sikao.app.adapter.MyAddsStepPricesListItemAdapter;
import com.sikao.app.adapter.MyAddsStepPricesListItemForStateThreeAdapter;
import com.sikao.app.bean.AddStepPricesBean;
import com.sikao.app.bean.AddStepPricesSubmitBean;
import com.sikao.app.bean.OwnGoodsDetailBean;
import com.sikao.app.dao.AddStepPricesModel;
import com.sikao.app.myinterface.Callback;
import com.sikao.app.net.OnNetBackListener;
import com.sikao.app.ui.base.BaseActivity;
import com.sikao.app.utils.JSONUtil;
import com.sikao.app.utils.Utils;
import com.sikao.app.view.MyListView;

import java.util.ArrayList;
import java.util.List;

/**
 * 阶梯价格
 */
public class AddStepPricesActivity extends BaseActivity implements View.OnClickListener, Callback {

	private TextView titleTv;
	private TextView title_right_tv;
	private ImageView backIv;
	private MyListView mll_add_step_prices;
	private Button bt_save;
	private String goodsUnit;
	private MyAddsStepPricesListItemAdapter myAddsStepPricesListItemAdapter;
	private MyAddsStepPricesListItemForStateThreeAdapter myAddsStepPricesListItemForStateThreeAdapter;
	private AddStepPricesSubmitBean addStepPricesSubmitBean = new AddStepPricesSubmitBean();
	public static List<AddStepPricesSubmitBean.StepPricesBean> stepPricesBeanList = new ArrayList<AddStepPricesSubmitBean.StepPricesBean>();

	private int releaseGoodsState = 1;
	List<OwnGoodsDetailBean.ResBean.StepPriceBean> stepPriceList;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		setContentView(R.layout.activity_add_step_prices);
		super.onCreate(savedInstanceState);

	}

	@Override
	protected void initData() {
		goodsUnit = getIntent().getStringExtra("goodsUnit");
		releaseGoodsState = getIntent().getIntExtra("releaseGoodsState", 1);
		if (releaseGoodsState == 3) {//详情里的数据
			stepPriceList = (List<OwnGoodsDetailBean.ResBean.StepPriceBean>) getIntent().getSerializableExtra("step_price");
		}
	}

	@Override
	protected void initView() {
		titleTv = (TextView) findViewById(R.id.title_middle_tv);
		title_right_tv = (TextView) findViewById(R.id.title_right_tv);
		mll_add_step_prices = (MyListView) findViewById(R.id.mll_add_step_prices);


		bt_save = (Button) findViewById(R.id.bt_save);
		backIv = (ImageView) findViewById(R.id.title_left_img);

		if (releaseGoodsState == 1) {//发布商品进入的
			myAddsStepPricesListItemAdapter = new MyAddsStepPricesListItemAdapter(this, stepPricesBeanList, this, goodsUnit);
			mll_add_step_prices.setAdapter(myAddsStepPricesListItemAdapter);
		}
		if (releaseGoodsState == 3) {//详情里的数据
			myAddsStepPricesListItemForStateThreeAdapter = new MyAddsStepPricesListItemForStateThreeAdapter(this, stepPriceList, this, goodsUnit);
			mll_add_step_prices.setAdapter(myAddsStepPricesListItemForStateThreeAdapter);
		}


	}

	@Override
	protected void initListener() {
		title_right_tv.setOnClickListener(this);
		backIv.setOnClickListener(this);
		bt_save.setOnClickListener(this);
	}

	@Override
	protected void init() {
		titleTv.setText("阶梯价格");
		backIv.setVisibility(View.VISIBLE);
		title_right_tv.setVisibility(View.VISIBLE);
		title_right_tv.setText("添加");

		if (releaseGoodsState == 3) {//查看详情进来的
			title_right_tv.setVisibility(View.GONE);
			bt_save.setVisibility(View.GONE);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
			case R.id.title_left_img:
				stepPricesBeanList.clear();
				finish();
				break;
			case R.id.title_right_tv://添加阶梯价格
				AddStepPricesSubmitBean.StepPricesBean stepPricesBean = new AddStepPricesSubmitBean.StepPricesBean();
				stepPricesBean.setNumber("0");
				stepPricesBean.setPrice("0");
				stepPricesBeanList.add(stepPricesBean);
				myAddsStepPricesListItemAdapter.notifyDataSetChanged();

				break;
			case R.id.bt_save://保存
				saveStepPrices();

				break;
			default:
				break;
		}
	}

	/**
	 * 获取阶梯价格的id
	 */
	private void saveStepPrices() {
		if (stepPricesBeanList.size() > 0) {
			addStepPricesSubmitBean.setStepPrices(stepPricesBeanList);
			new AddStepPricesModel(this, new OnNetBackListener<AddStepPricesBean>() {
				@Override
				public void netBack(AddStepPricesBean data) {
					if (data == null) {
						Toast.makeText(AddStepPricesActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
					} else {
						if (data.getStatus() == 1) {
							String ids = data.getRes().getIds();
							Intent intent = new Intent();
							intent.putExtra("stepPrice", ids);
							intent.putExtra("isUseStepPrice", true);
							setResult(ReleaseGoodsActivity.STEP_PRICE_RESULT_CODE, intent);
							finish();
						} else {
							Toast.makeText(AddStepPricesActivity.this, data.getMsg(), Toast.LENGTH_SHORT).show();
						}
					}
				}
			}).request(JSONUtil.toJSON(addStepPricesSubmitBean), true);
		} else {
			Intent intent = new Intent();
			intent.putExtra("stepPrice", "");
			intent.putExtra("isUseStepPrice", false);
			setResult(ReleaseGoodsActivity.STEP_PRICE_RESULT_CODE, intent);
			finish();
		}
	}


	@Override
	public void notice(int position) {
		//删除
		Utils.showLog("====删除的是" + position);
		stepPricesBeanList.remove(position);
		myAddsStepPricesListItemAdapter.notifyDataSetChanged();
	}
}
